package client;

import static debug.Debug.DBUG;
import java.awt.*;
import java.io.IOException;
import java.util.*;
import java.util.List;

import javax.swing.*;

public class ClientMap extends JPanel {
	private ImageIcon[][] images; 
	private static final int WIDTH = 20;
	private static final int HEIGHT = 20;
	private static final int TILE_SIZE = 18;
	private List<ClientChangeable> changeables;
	
	public ClientMap() {
		changeables = new ArrayList<ClientChangeable>();
		images = getImages();
		this.setPreferredSize(new Dimension(WIDTH*TILE_SIZE, HEIGHT*TILE_SIZE));
		new Thread(new updater()).start();
	}
	
	public ImageIcon[][] getImages() {
		MapReader reader = new MapReader("map");
		return reader.getImages();
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		for (int x = 0; x < WIDTH; x++) {
			for (int y = 0; y < HEIGHT; y++) {
				g.drawImage(images[x][y].getImage(), x*TILE_SIZE, y*TILE_SIZE, null);
			}
		}	
		for(ClientChangeable changedObj: changeables){
			changedObj.draw(g);
		}
	}
	
	public void addChangeable(ClientChangeable changedObj){
	//	DBUG(changeables.size());
		if(changeables.contains(changedObj)){
			changeables.set(changeables.indexOf(changedObj),changedObj);
		}else{
			changeables.add(changedObj);
		}
	}
	private class updater implements Runnable{
		@Override
		public void run() {
			while (true) {
				repaint();
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {}
			}
		}
		
	}

}
